/***************************************************************************
 *   This file is part of KTagger                                          *
 *   Copyright (C) 2010 by Luca Bellonda                                   *
 *   lbellonda _at_ gmail.com                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include "configurationManager.h"
#include "Utility.h"
#include <QStringList>
#include <QVector>
#include <KLocale>

#define TAG_SEPARATOR   ","
#define SOURCE_CLASS "configurationManager"

ConfigurationManager::ConfigurationManager()
{
    initLogger();
}

ConfigurationManager::~ConfigurationManager()
{
    logger.debug(&logParam, "end");
}

bool ConfigurationManager::init()
{
    logger.debug(&logParam, "started");
    if (!db.init()) {
        logger.error(&logParam, "error init db");
        return false ;
    }
    return true;
}


void ConfigurationManager::initLogger()
{
    logParam.source = SOURCE_CLASS;
}

//-------------------------- Operations -----------------------------

//TODO:
QStringList ConfigurationManager::getLastUsedTags()
{
    logger.debug(&logParam, "getLastUsedTags()");
    QStringList tagList ;
#ifdef  MOCKUP
    tagList.append("Java - MOCKUP");
    tagList.append("Programming- MOCKUP");
    tagList.append("Auto - MOCKUP");
    tagList.append("Navi - MOCKUP");
    return tagList ;
#endif
    tagList = db.getLastUsedTags();
    return tagList ;
}

QStringList ConfigurationManager::getCompanionTags(const QString &tag)
{
    logger.debug(&logParam, "getCompanionTags()");
    QStringList tagList ;
#ifdef  MOCKUP
    tagList.append("Flex - MOCKUP");
    tagList.append("Javascript - MOCKUP");
    tagList.append("Motocross - MOCKUP");
    tagList.append("KDE - MOCKUP");
    return tagList ;
#endif
    tagList = db.getCompanionTags(tag);
    return tagList;
}


bool ConfigurationManager::saveTagSetConfiguration(const QStringList &confirmedTags)
{
    bool isOk = true ;
    logger.debug(&logParam, "saveTagSetConfiguration()");
    QStringList tagList = confirmedTags ;
    tagList.sort();
    foreach(QString tag, confirmedTags) {
        if (!db.saveTagMRU(tag)) {
            isOk = false;
            logger.warning(&logParam, "error saving tags");
            break;
        }
    }

    QString tagGroup = tagList.join(TAG_SEPARATOR);
    tagGroup.append(",");
    if (!db.saveTagSetConfiguration(tagGroup)) {
        Utility::error(i18n("Error saving statistics."));
        isOk = false;
    }
    return isOk;
}

QVector<TagProfile*> *ConfigurationManager::getProfilesList()
{
#ifdef  MOCKUP
    if (true) {
        static const char* p[5] = {"Musica", "Lavoro", "Viaggi", "Programmazione", "C++"};
        QVector<TagProfile*> *list = new QVector<TagProfile*>();
        if (NULL != list) {
            for (int i = 0; i < 5 ; i ++) {
                TagProfile *tag = TagProfile::getMockUp(p[i]);
                list->append(tag);
            }
        }
        return list;
    }
#endif
    logger.debug(&logParam, "getProfilesList");
    QVector<TagProfile*>*list = db.getProfileList();
    if (NULL != list) {
        logger.debug(&logParam, "getProfilesList ok");
    } else {
        logger.error(&logParam, "getProfilesList KO");
    }
    return list;
}

TagProfile *ConfigurationManager::createNewProfile()
{
    logger.debug(&logParam, "create new profile");
#ifdef  MOCKUP
    return TagProfile::getMockUp(i18n("nuovo"));
#endif
    TagProfile *newProfile = new TagProfile(i18n("new profile"));
    if (NULL != newProfile) {
        if (db.insertProfile(newProfile)) {
            logger.debug(&logParam, "profile created");
            return newProfile;
        }
        delete newProfile;
    }
    logger.error(&logParam, "unable to create profile ");
    return NULL ;
}

bool ConfigurationManager::saveProfile(TagProfile *profile)
{
    logger.debug(&logParam, "save profile");
    bool isOk = db.updateProfile(profile);
    if (isOk) {
        logger.debug(&logParam, "saving profile ok");
    } else {
        logger.error(&logParam, "error saving profile");
    }
    return isOk;
}

bool ConfigurationManager::saveCurrentProfile(TagProfile *profile)
{
    logger.debug(&logParam, "save current profile");
    bool isOk = db.saveCurrentProfile(profile);
    if (isOk) {
        logger.debug(&logParam, "saving current profile ok");
    } else {
        logger.error(&logParam, "error saving current profile");
    }
    return isOk;
}


bool ConfigurationManager::getCurrentProfileId(QString &result)
{
    logger.debug(&logParam, "getCurrentProfileId");
    return db.getCurrentProfileId(result);
}

QVector<TagProfile*> *ConfigurationManager::getProfilesListLight()
{
    logger.debug(&logParam, "getProfilesListLight");
    QVector<TagProfile*>*list = db.getProfileListLight();
    if (NULL != list) {
        logger.debug(&logParam, "getProfilesListLight ok");
    } else {
        logger.error(&logParam, "getProfilesListLight KO");
    }
    return list;
}

TagProfile* ConfigurationManager::getProfile(const QString &id)
{
    logger.debug(&logParam, "getProfile");
    TagProfile* profile = db.getProfile(id);
    if (NULL != profile) {
        logger.debug(&logParam, "getProfile ok");
    } else {
        logger.error(&logParam, "getProfile KO");
    }
    return profile;
}

QStringList ConfigurationManager::getProfileTags(const QString & id, bool &isOk)
{
    logger.debug(&logParam, "getProfileTags");
    QStringList result = db.getProfileTags(id, isOk);
    if (!isOk) {
        logger.debug(&logParam, "getProfileTags ok");
    } else {
        logger.error(&logParam, "getProfileTags KO");
    }
    return result;
}

QStringList ConfigurationManager::getActivityTags(const QString & activityName, bool &isOk)
{
    logger.debug(&logParam, "getActivityTags");
    QStringList result = db.getActivityTags(activityName, isOk);
    if (!isOk) {
        logger.debug(&logParam, "getActivityTags ok");
    } else {
        logger.error(&logParam, "getActivityTags KO");
    }
    return result;
}
